package com.atguigu.gmall.payment.config;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;


@Configuration
public class AlipayConfig {

    // 获取配置文件中的数据
    @Value("${alipay_url}")
    private String alipay_url;

    @Value("${app_private_key}")
    private String app_private_key;

    @Value("${app_id}")
    private String app_id;  // app_id = app_id{2021001163617452}


    public final static String format="json";
    public final static String charset="utf-8";
    public final static String sign_type="RSA2";

    //  返回的同步回调地址
    public static String return_payment_url;
    //  获取appId
//    public static String app_id;
    //  返回异步回调地址
    public static String notify_payment_url;
    //  当同步回调成功之后，给用户展示哪个页面！
    public static String return_order_url;
    //  支付宝公钥
    public static String alipay_public_key;

//    @Value("${app_id}")
//    public void setAppId(String app_id) {
//        AlipayConfig.app_id = app_id;
//    }

    //  如果类中有变量使用了static 修饰，则不能直接使用@value 注解获取数据！ 封装一个方法先获取到配置文件的数据！在赋值给静态变量！
    //  用静态修饰方便后面获取这个变量的数据！
    @Value("${alipay_public_key}")
    public void setAlipay_public_key(String alipay_public_key) {
        AlipayConfig.alipay_public_key = alipay_public_key;
    }

    @Value("${return_payment_url}")
    public  void setReturn_url(String return_payment_url) {
        AlipayConfig.return_payment_url = return_payment_url;
    }

    @Value("${notify_payment_url}")
    public   void setNotify_url(String notify_payment_url) {
        AlipayConfig.notify_payment_url = notify_payment_url;
    }

    @Value("${return_order_url}")
    public   void setReturn_order_url(String return_order_url) {
        AlipayConfig.return_order_url = return_order_url;
    }

    // 将 AlipayClient 注入到spring 容器中。
    // <bean id="alipayClient" class="com.alipay.api.AlipayClient"> </bean>
    @Bean
    public AlipayClient alipayClient(){
        // AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE); //获得初始化的AlipayClient
        AlipayClient alipayClient=new DefaultAlipayClient(alipay_url,app_id,app_private_key,format,charset, alipay_public_key,sign_type );
        return alipayClient;
    }
}
